<template>
  <aside class="sidebar">
    <div class="sidebar-header">
      <h2>AI问答系统</h2>
      <button class="new-chat-btn" @click="handleNewChat">
        <span>+ 新对话</span>
      </button>
    </div>

    <div class="conversation-list">
      <div v-if="conversations.length === 0" class="empty-tip">
        <div class="empty-content">
          <span>暂无对话</span>
        </div>
      </div>
      <template v-else>
        <div
            v-for="conversation in conversations"
            :key="conversation.id"
            class="conversation-item"
            :class="{ active: activeId === conversation.id }"
            @click="selectConversation(conversation.id)"
        >
          <span>{{ conversation.name || '新对话' }}</span>
        </div>
      </template>
    </div>
  </aside>
</template>

<script>
import {ref} from 'vue'
import {useRouter} from 'vue-router'

export default {
  props: {
    conversations: {
      type: Array,
      required: true,
      validator: (value) => {
        return Array.isArray(value)
      },
      default: () => []
    }
  },
  emits: ['select', 'new-chat'],
  setup(props, {emit}) {
    const router = useRouter()
    const activeId = ref(null)

    const selectConversation = (id) => {
      activeId.value = id
      emit('select', id)
    }

    const handleNewChat = () => {
      activeId.value = null
      emit('new-chat')
      router.push({name: 'AiHomeView'})
    }

    return {
      activeId,
      selectConversation,
      handleNewChat
    }
  }
}
</script>

<style scoped>
.sidebar {
  width: 260px;
  height: 100vh;
  background-color: #fff;
  border-right: 1px solid #e8e8e8;
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  padding: 1rem;
  border-bottom: 1px solid #e8e8e8;
}

.sidebar-header h2 {
  margin: 0 0 1rem 0;
  font-size: 1.2rem;
  color: #333;
  font-weight: 600;
}

.new-chat-btn {
  width: 100%;
  padding: 0.5rem;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.3s;
}

.new-chat-btn:hover {
  background-color: #40a9ff;
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
  display: flex;
  flex-direction: column;
}

.empty-tip {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.empty-content {
  padding: 0.75rem;
  color: #999;
  font-size: 0.9rem;
  text-align: center;
}

.conversation-item {
  padding: 0.75rem;
  border-radius: 4px;
  cursor: pointer;
  margin-bottom: 0.25rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background-color 0.3s;
}

.conversation-item:hover {
  background-color: #f5f5f5;
}

.conversation-item.active {
  background-color: #e6f7ff;
  color: #1890ff;
  font-weight: 500;
}
</style>